/**
 * 采购申请（添加|修改）purchaseApplyOption.js
 * 版本号： v1.0.0
 */
$(function(){
    //设置输入框的最大限制长度
    $(".easyui-textbox").each(function(){
        var data_options = $(this).attr("data-options");
        if(data_options){
            var matchs = data_options.match(/length\[1,(\d*)\]/);
            if(matchs){
                $(this).textbox("textbox").attr("maxlength", matchs[1] - 0 + 1);
            }else{
                $(this).textbox("textbox").attr("maxlength", 20);
            }
        }else{
            $(this).textbox("textbox").attr("maxlength", 20);
        }
    });

    //加载合同项目下拉框
    var coId = $("#coId").val();
    if(null==coId&&''==coId){
        $.ajax({
            url: CONTEXT_PATH + "/contract/getContractListForCombo.do",
            type: "get",
            success: function (data) {
                var themecombo = "";
                if(data.length > 2){
                    themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                    themecombo += data.substring(1);
                }else{
                    themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                }
                themecombo = JSON.parse(themecombo);
                $("#contractList").combobox("loadData",themecombo);
                $("#contractList").combobox({
                    valueField:'id',
                    textField:'text',
                    onSelect:function(node){
                        $('#coId').val(node.id);
                    }
                });
                $("#contractList").combobox("setValue","");
            }
        });
    }else{
        $.ajax({
            url: CONTEXT_PATH + "/contract/getContractListForCombo.do",
            type: "get",
            success: function (data) {
                var themecombo = "";
                if(data.length > 2){
                    themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                    themecombo += data.substring(1);
                }else{
                    themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                }
                themecombo = JSON.parse(themecombo);
                $("#contractList").combobox("loadData",themecombo);
                $("#contractList").combobox({
                    valueField:'id',
                    textField:'text',
                    onSelect:function(node){
                        $('#coId').val(node.id);
                    }
                });
                $("#contractList").combobox("setValue",coId);
            }
        });
    }


    /**
     * 合同类型为合同外时，必须上传附件
     */
    $("#purType").combobox({
        onChange:function (data) {
            var fileName = $("#fileName").val();
               if(data!=2&&purchaseDetailList){
                   $.messager.show({ title:'提示', msg: '合同外不可修改为合同内!', timeout:3000, showType:"slide" });
                   $("#purType").combobox("setValue","");
                   return;
               }

            if(2==data&&''==fileName){
                $("#uploadfile").filebox("textbox").validatebox({
                   required:true
                });
            }else{
                $("#uploadfile").filebox("textbox").validatebox({
                    required:false
                });
            }
        }
    });

    //上传操作
    $('#uploadBtn').bind('click', function(){
            uploadFile('uploadBtn', 'uploadfile', 'fileName', 'fileId', 'url', 'size', 'fix', 'md5', '');
    });

    //图片浏览
    $('#browseBtn').bind('click', function(){
        var fileId = $('#fileId').val();
        if(fileId != null && fileId != ""){
            $.ajax({
                url: CONTEXT_PATH + "/commonFile/getFileUrlById.do?id=" + fileId,
                type: "get",
                success: function (data) {
                    var url = data.message;
                    var imgDiv = $('#browseBtn');
                    if(url == "" || url == null){
                        imgDiv.attr('src',"");
                        $.messager.show({ title:'提示', msg: '服务器文件已不存在!', timeout:3000, showType:"slide" });
                        return;
                    }else if(!valiImgType("fileName")){
                        imgDiv.attr('src',"");
                        $.messager.show({ title:'提示', msg: '该文件不是图片格式，不可浏览!', timeout:3000, showType:"slide" });
                        return;
                    }else{
                        imgDiv.attr('src',url);
                    }
                    imgDiv.trigger("custom");
                }
            });
        }else{
            $.messager.show({ title:'提示', msg: '请上传文件后再浏览!', timeout:3000, showType:"slide" });
            return;
        }
    });


    //修改时填入文件名称
    var fileName = $('#fileName').val();
    if(fileName != null && fileName != ""){
        $('#uploadfile').filebox({prompt:fileName});
    }

    /**
     * 保存按钮
     */
    $('#optionPurchaseApply').bind('click', function(){
        var fileId = $("#fileId").val();
        var flag = $("#purType").combobox("getValue");
        if(flag == 2){
            if(null == fileId || "" == fileId){
                $.messager.show({ title:'提示', msg: '文件未上传！', timeout:3000, showType:"slide" });
            }else{
                doOptionPurchaseApplyMethod();
            }
        }else{
            doOptionPurchaseApplyMethod();
        }
    });
});

/**
 * 执行保存操作方法
 */
function doOptionPurchaseApplyMethod() {
    var params = $("#operate_form").serialize();
    $("#operate_form").form("submit", {
        cache: false,
        data: params,
        url: CONTEXT_PATH + "/purchaseApply/save.do",
        onSubmit: function () {
            /**
             * preventSubmit('表单id')
             */
            preventSubmit('operate_form','optionPurchaseApply');
            return $(this).form("validate");
        },
        success: function (data) {
            var obj = JSON.parse(data);
            var url = CONTEXT_PATH + "/purchaseApply/index.do";
            saveJump(obj,url);
        }
    });
}

function uploadFile(uploadBtn, uploadfile, fileName, fileId, url, size, fix, md5, showCtrl){
    if($("#" + uploadBtn).linkbutton('options').disabled == false){
        var file = $("#" + uploadfile).filebox('getValue');
        if("" == file){
            $.messager.show({ title:'提示', msg: '请选择要上传附件！', timeout:3000, showType:"slide" });
            return false;
        }
        //获取上传文件名称
        var getfileName = file.substring(file.lastIndexOf("\\") + 1);
        //获取文件后缀
        var suffix = getfileName.substring(getfileName.lastIndexOf(".") + 1).toUpperCase();
        var dataTypeStr = "json";
        if(isIE()){
            //datatype需为text，不能为text/html;否则ie兼容模式上传会失败
            dataTypeStr = "text";
        }else{
            dataTypeStr = "json";
        }
        $('#operate_form').ajaxSubmit({
            url: CONTEXT_PATH + "/upload/uploadFile.do",
            dataType: dataTypeStr,
            type: "post",
            clearForm: false,
            beforeSubmit : function() {
                $.messager.progress({
                    title: '提示',
                    msg: '附件上传中, 请稍候...',
                    text: ''
                });
            },
            complete : function(){
                $.messager.progress('close');
            },
            success : function(data, statusText) {
                //解决IE下载问题
                if(isIE()){
                    data = toJson(data);
                }
                //回显示控件
                if(null != showCtrl &&  "" != showCtrl){
                    $("#" + showCtrl).attr('src', data.result.url);
                }
                //禁用按钮
                $("#" + uploadBtn).linkbutton({ disabled:true });
                //消息提示
                messager(data);
                //赋值元素
                if("PC_200" == data.code){
                    $("#" + fileId).val(data.result.fileId);
                    $("#" + fileName).val(getfileName);
                    $("#" + url).val(data.result.url);
                    $("#" + size).val(data.result.size);
                    $("#" + fix).val(data.result.fix);
                    $("#" + md5).val(data.result.md5);
                }
            },
            error : function(xhr, status, err) {
                $.messager.show({ title:'提示', msg: '附件上传失败！', timeout:3000, showType:"slide" });
            }
        });
        return false;
    }
}
